/*
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-09-16 10:45:56
 * @LastEditTime: 2020-02-22 16:27:45
 * @LastEditors: Please set LastEditors
 */
var DB = function () {
    return {
        dropTable() {
            var db = api.require('db');
            db.executeSql({
                name: 'FLDCSAPP',
                sql: 'drop table Chat'
            }, function (ret, err) {
                if (ret.status) {
                    alert('删除成功')
                }
            });
        },
        initDatabase(callback) {
            var db = api.require('db');
            db.openDatabase({
                name: 'FLDCSAPP'
            }, function (ret, err) {
                if (ret.status) {
                    //数据库创建成功
                    if (callback) callback();
                } else {
                    //初始化失败
                    layer.open({
                        content: '初始化失败 请稍后重试',
                        skin: 'msg',
                        time: 2
                    });
                }
            });
        },
        createTable(callback) {
            var db = api.require('db');
            // //创建草稿表
            // db.selectSql({
            //     name: 'FLDCSAPP',
            //     sql: 'select * from sqlite_master where type="table" and name = "Draft"'
            // }, function (ret, err) {
                
            //     if (ret.status) {
            //         if (!ret.data.length) {
            //             db.executeSql({
            //                 name: 'FLDCSAPP',
            //                 sql: 'create table Draft(DraftID integer primary key autoincrement, JSONStr1 text, JSONStr2 text, JSONStr3 text, JSONStr4 text, CreateTime datetime, UpdateTime datetime, Type int, Status int, Remark varchar(255))'
            //             }, function (ret, err) {
            //                 if (ret.status) {
            //                     //表创建成功
            //                     if (callback) callback();
            //                 }
            //             });
            //         }
            //     }
            // });
            // //创建聊天表
            // db.selectSql({
            //     name: 'FLDCSAPP',
            //     sql: 'select * from sqlite_master where type="table" and name = "Chat"'
            // }, function (ret, err) {
            //     if (ret.status) {
            //         if (!ret.data.length) {
            //             db.executeSql({
            //                 name: 'FLDCSAPP',
            //                 sql: 'create table Chat(ChatID integer primary key autoincrement, UserID int, JSONStr1 text, JSONStr2 text, JSONStr3 text, JSONStr4 text, CreateTime datetime, UpdateTime datetime, Type int, Status int, Remark varchar(255))'
            //             }, function (ret, err) {
            //                 if (ret.status) {
            //                     //表创建成功
            //                     if (callback) callback();
            //                 }
            //             });
            //         }
            //     }
            // });

            //创建首页图片缓存表
            db.selectSql({
                name: 'FLDCSAPP',
                sql: 'select * from sqlite_master where type="table" and name = "ImageCache"'
            }, function (ret, err) {
                if (ret.status) {
                    if (!ret.data.length) {
                        db.executeSql({
                            name: 'FLDCSAPP',
                            sql: 'create table ImageCache(ImageCacheID integer primary key autoincrement, Url1 text, Url2 text, Type int, Status int, Remark varchar(255))'
                        }, function (ret, err) {
                            if (ret.status) {
                                //表创建成功
                                if (callback) callback();
                            }
                        });
                    }
                }
            });
        },
        selectSql(sql, callback) {
            var db = api.require('db');
            db.selectSql({
                name: 'FLDCSAPP',
                sql: sql
            }, function (ret, err) {
                if (ret.status) {
                    if (callback) callback(ret.data)
                }
            });
        },
        executeSql(sql, callback) {
            var db = api.require('db');
            console.log(sql)
            db.executeSql({
                name: 'FLDCSAPP',
                sql: sql
            }, function (ret, err) {
                if (ret.status) {
                    if (callback) callback()
                } else {
                    alert(JSON.stringify(err))
                }
            });
        },
        selectSqlSync(sql) {
            var db = api.require('db');
            var sqlResult = db.selectSqlSync({
                name: 'FLDCSAPP',
                sql: sql
            });
            if (sqlResult.status) {
                return sqlResult.data;
            } else {
                return null;
            }
        },
        executeSqlSync(sql) {
            var db = api.require('db');
            
            var sqlResult = db.executeSqlSync({
                name: 'FLDCSAPP',
                sql: sql
            });
            if (sqlResult.status) {
                return sqlResult.data;
            } else {
                return null;
            }
        }

    }
}()